<template>
	<view class="content">
		<NavTabbar activeTab="user"></NavTabbar>
		<scroll-view scroll-y="true" class="scroll-view_list">
			<view class="navbar">
				<view class="fixed">
					<!-- 设置占位 将微信小程序的状态栏高度预留出来 -->
					<view :style="{height: navbarStatusHeight + 'px'}"></view>
					<view
						class="navbar-content"
						:style="{width: navBarWidth + 'px', height: navBarHeight + 'px'}"
					></view>
				</view>
				<!-- <view :style="{height:navbarStatusHeight+navBarHeight+'px' }"></view> -->
			</view>
			<view class="conten_block">
				<view class="userlogo">
					<!-- <image style="width: 100%;height: 40rpx;" mode="aspectFit" src="/pages_user/img/logo_bg.png">
					</image> -->
					<view class="logo_top">
						<view class="img">
							<image
								mode="aspectFill"
								class="user_img"
								v-if="store.state.userInfo.avatar"
								:src="store.state.userInfo.avatar"
								@click="lookPic(store.state.userInfo.avatar)"
							/>
							<uni-icons
								size="100"
								color="#007cff"
								v-else
								type="contact"
							></uni-icons>
						</view>
						<view class="level_name">
							<view class="name">
								{{ store.state.userInfo.name }}
							</view>
							<view class="edit" @click="goEdit">
								<uni-icons
									size="14"
									type="compose"
									class="ed_icon"
									color="#000"
								></uni-icons>
								编辑资料
							</view>
						</view>
					</view>
				</view>

				<view class="showData">
					<view
						class="item"
						v-for="(item, index) in option"
						:key="index"
						@click="goPages(index)"
					>
						<image :src="item.icon" mode="aspectFit" class="img_y"></image>
						<view class="text-black">{{ item.title }}</view>
					</view>
				</view>
				<view class="card">
					<uni-list class="card_li">
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							@click="goAcl"
							link
							title="换绑手机"
							:extraIcon="{color: '#454757', size: '20', type: 'phone-filled'}"
							:rightText="store.getters.setProcessingTel"
						/>
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							:extraIcon="{color: '#454757', size: '20', type: 'weixin'}"
							title="绑定微信"
							rightText="未绑定"
						/>
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							@click="popWx"
							:clickable="true"
							:extraIcon="{
								color: '#454757',
								size: '20',
								type: 'notification-filled',
							}"
							title="微信通知"
							rightText="未开启"
							v-if="open"
						/>
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							@click="popWx"
							:clickable="true"
							:extraIcon="{
								color: '#454757',
								size: '20',
								type: 'notification-filled',
							}"
							title="微信通知"
							rightText="已开启"
							v-else
						/>
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							link
							@click="bmcAddress"
							:extraIcon="{
								color: '#454757',
								size: '20',
								type: 'location-filled',
							}"
							title="地址管理"
						/>
						<uni-list-item
							:show-extra-icon="true"
							showArrow
							@click="gochangePwd"
							link
							:extraIcon="{color: '#454757', size: '20', type: 'locked-filled'}"
							title="修改密码"
						/>
					</uni-list>
				</view>
				<view class="btn_logout">
					<button
						v-if="store.state.userMobileHasLogin || token"
						type="primary"
						size="small"
						class="onlogout"
						@click="onLogout"
					>
						<!-- <image src="@/static/user/out.png" mode="aspectFill" class="img_btn"></image> -->
						<span class="text_btn">退出登录</span>
					</button>
					<button
						v-else
						type="primary"
						size="small"
						class="onlogout"
						@click="goLogin"
					>
						<span class="text_btn">去登录</span>
					</button>
				</view>
			</view>
		</scroll-view>

		<!-- 微信通知弹窗 -->
		<uni-popup ref="wx_notif" type="center">
			<view class="wx_notif">
				<view class="po_wx">
					<view class="titlt_wx">微信消息通知</view>
					<view class="span_wx">
						开启微信消息通知
						<br />
						实时获取工单进度
					</view>
					<view class="btn_wx">
						<button type="primary" class="onlogout" @click="btnOpen(true)">
							立即开启
						</button>
					</view>
					<view class="span_wx" @click="btnOpen(false)">暂不开启</view>
				</view>
			</view>
		</uni-popup>
		<!-- 退出登录弹窗 -->
		<uni-popup ref="logoutDialog" type="dialog">
			<uni-popup-dialog
				cancelText="否"
				confirmText="是"
				title="通知"
				content="是否退出登录?"
				@confirm="dialogConfirm"
				@close="dialogClose"
			></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import {ref, onMounted} from 'vue';
	import {onLoad, onShow} from '@dcloudio/uni-app';
	import apiLogin from '@/util/api/login.js';
	import store from '@/store/index.js';
	import NavTabbar from '@/components/u-user/nav-tabbar.vue';
	const token = ref(''); //获取token值
	onShow(() => {
		token.value = uni.getStorageSync('access-user').token;
	});
	const navbarStatusHeight = ref(0);
	const navBarHeight = ref(0);
	const navBarWidth = ref(375);
	const wx_notif = ref();
	const logoutDialog = ref();
	const open = ref(true);
	// const option = ref([{
	// 	title: '我的工单',
	// 	icon: '/static/user/y_w.svg'
	// }, {
	// 	title: '我的商品',
	// 	icon: '/static/user/y_p.svg'
	// }])
	const option = ref([
		{
			title: '我的工单',
			icon: '/static/home/myorder.svg',
		},
		{
			title: '我的商品',
			icon: '/static/home/mygoods.svg',
		},
	]);
	onMounted(() => {
		const info = uni.getSystemInfoSync();
		navbarStatusHeight.value = info.statusBarHeight;
		// 根据微信小程序的右上侧的胶囊样式 设置导航栏内容的高度
		// 条件编译 以下三个不支持胶囊样式
		// #ifndef H5 || APP-PLUS || MP-ALIPAY
		const menuInfo = uni.getMenuButtonBoundingClientRect();
		// 导航栏内容的高度 = (胶囊底部的高度 - 状态栏的高度) + (胶囊顶部的高度 - 状态栏的高度)
		navBarHeight.value =
			menuInfo.bottom -
			info.statusBarHeight +
			(menuInfo.top - info.statusBarHeight);
		// 导航栏内容的宽度  除开胶囊按钮左侧的内容宽度
		navBarWidth.value = menuInfo.left;
		// #endif
	});
	onLoad(async () => {
		//显示加载框
		uni.showLoading({
			title: '加载中',
		});
		// 获取用户信息
		await store.dispatch('getUpdataUser');
		// //隐藏加载框
		uni.hideLoading();
	});
	//退出
	const onLogout = () => {
		logoutDialog.value.open();
	};
	// 确认退出登录
	const dialogConfirm = () => {
		uni.navigateTo({
			url:
				'/pages/pages_user/login/login?custCode=' +
				uni.getStorageSync('custCode'),
		});

		apiLogin.logout().then((res) => {
			if (res.code == 20000) {
				uni.removeStorageSync('access-user');
				// uni.showToast({
				// 	icon: 'success',
				// 	title: res.message
				// })
			}
		});
	};
	// 去登录
	const goLogin = () => {
		uni.navigateTo({
			url:
				'/pages/pages_user/login/login?custCode=' +
				uni.getStorageSync('custCode'),
		});
		uni.removeStorageSync('access-user');
	};
	// 关闭弹窗
	const dialogClose = () => {
		logoutDialog.value.close();
	};
	// 绑定手机
	const goAcl = () => {
		if (getLoginSet())
			uni.navigateTo({
				url: '/pages_user/user/acl',
			});
	};
	// 预览图片
	const lookPic = (list) => {
		if (!list) return;
		uni.previewImage({
			urls: [list],
		});
	};
	// 编辑信息
	const goEdit = () => {
		if (getLoginSet())
			uni.navigateTo({
				url: '/pages_user/user/edit',
			});
	};
	// 修改密码
	const gochangePwd = () => {
		if (getLoginSet())
			uni.navigateTo({
				url: '/pages_user/user/pwd',
			});
	};
	// 地址管理
	const bmcAddress = () => {
		if (getLoginSet()) {
			uni.navigateTo({
				url: '/pages_user/user/address',
			});
		}
	};
	// 微信信息弹窗
	const popWx = () => {
		wx_notif.value.open();
	};
	//开启信息通知
	const btnOpen = (val) => {
		wx_notif.value.close();
		if (val) {
			open.value = false;
		} else {
			open.value = true;
		}
	};
	// 0==我的工单 1==我的商品 2==激活质保卡
	const goPages = (val: any) => {
		switch (val) {
			case 0:
				if (getLoginSet())
					uni.navigateTo({
						url: '/pages_user/order/index',
					});
				break;
			case 1:
				if (getLoginSet())
					uni.navigateTo({
						url: '/pages_user/goods/goods',
					});
				break;
		}
	};
	// 判断是否登录
	const getLoginSet = () => {
		if (!token.value) {
			uni
				.showModal({
					title: '提示',
					content: '您没登录，请先登录！',
				})
				.then((res) => {
					//确认
					if (res.confirm) {
						//关闭所有页面，打开到应用内的某个页面
						uni.reLaunch({
							url:
								'/pages/pages_user/login/login?custCode=' +
								uni.getStorageSync('custCode'),
						});
						uni.removeStorageSync('access-user');
						return false;
					}
				});
		} else {
			return true;
		}
	};
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fafafa;
		height: 100vh;
		position: relative;
		background-image: url('~@/pages_network/img/background-image.jpg');
		background-repeat: no-repeat;
		background-size: cover;
		background-position: center;
		display: flex;
		flex-direction: column;
		overflow: hidden;

		.scroll-view_list {
			height: calc(100% - 120rpx);

			::-webkit-scrollbar {
				width: 0;
				height: 0;
				background-color: transparent;
				display: none;
			}
		}

		:deep(.uni-modal__bd) {
			color: #f56c6c !important;
		}

		.conten_block {
			width: 100%;
			display: flex;
			flex-direction: column;
			row-gap: 30rpx;
		}

		.userlogo {
			/* #ifndef MP-WEIXIN */
			padding-top: 30rpx;

			/* #endif */
			.logo_top {
				display: flex;
				padding: 20rpx 40rpx 0;

				.img {
					width: 110rpx;
					height: 110rpx;
					border-radius: 50%;
					margin-right: 30rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					overflow: hidden;
					background-color: #ebf9ff;

					.user_img {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}
				}

				.level_name {
					display: flex;
					align-items: center;
					flex-direction: column;
					justify-content: center;
					margin-right: 20rpx;

					.name {
						color: #000;
						font-size: 34rpx;
						margin-right: 14rpx;
					}

					.edit {
						font-size: 24rpx;
						color: #000;
						margin-top: 10rpx;

						.ed_icon {
							margin-right: 14rpx;
						}
					}
				}
			}
		}

		.card {
			margin: 0 40rpx;
			border-radius: 10rpx;
			padding: 0 24rpx;
			background-color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
			box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

			:deep(.card_li) {
				flex: 1;

				.uni-list-item__content-title {
					font-size: 28rpx;
				}
			}
		}

		.showData {
			display: flex;
			justify-content: space-evenly;
			height: 150rpx;
			margin: 0rpx 40rpx;
			// top: 20rpx;
			// padding: 24rpx 0;
			position: relative;
			box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

			&::after {
				content: '';
				width: 98%;
				position: absolute;
				top: -38rpx;
				height: 40%;
				// background-color: #cbcdda;
				border-radius: 60%;
			}

			// &::before {
			// 	content: '';
			// 	width: 40%;
			// 	position: absolute;
			// 	top: -50rpx;
			// 	height: 50%;
			// 	background-image: url('../img/logo_bg.png');
			// 	background-position: center;
			// 	background-repeat: no-repeat;
			// 	background-size: contain;
			// 	z-index: 2;
			// }

			background-color: #fff;
			border-radius: 10rpx;
			left: 0;
			z-index: 2;

			.item {
				flex: 1;
				text-align: center;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
				padding-top: 10rpx;

				.img_y {
					width: 60rpx;
					height: 60rpx;
				}

				.text-black {
					font-size: 28rpx;
					color: rgba(70, 74, 87, 1);
				}
			}
		}

		// .level {
		// 	position: relative;
		// 	// border-radius: 30rpx;
		// 	height: 40rpx;
		// 	top: 40rpx;
		// 	z-index: 999;
		// 	width: 100%;
		// 	margin: 0 auto;
		// 	width: 70%;

		// 	.vip {
		// 		display: flex;
		// 		align-items: center;
		// 		padding: 20rpx;

		// 		.img {
		// 			width: 30rpx;
		// 			height: 30rpx;
		// 			border-radius: 30rpx;
		// 			margin-right: 10rpx;
		// 		}

		// 		.title {
		// 			color: #f2a922;
		// 			font-size: 26rpx
		// 		}
		// 	}
		// }

		.btn_logout {
			// position: absolute;
			// bottom: -160rpx;
			margin: 0 auto;
			width: 100%;
			z-index: 5;

			:deep(button:after) {
				border: none !important;
			}

			.onlogout {
				font-size: 28rpx;
				// padding: 0rpx 200rpx;
				// margin: 0 40rpx;
				height: 80rpx;
				// border-radius: 50rpx;
				line-height: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #ffffff;
				margin: 40rpx 40rpx 0;
				box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

				.text_btn {
					font-weight: bold;
					color: #333;
				}

				.img_btn {
					width: 40rpx;
					height: 40rpx;
				}
			}
		}

		.wx_notif {
			width: 627rpx;
			height: 833rpx;
			position: relative;

			&::after {
				content: '';
				position: absolute;
				top: 0;
				width: 100%;
				height: 100%;
				z-index: -1;
				background-image: url('@/static/wx_pop.png');
				background-repeat: no-repeat;
				background-size: cover;
			}

			.po_wx {
				height: 460rpx;
				position: absolute;
				bottom: 0;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;

				.titlt_wx {
					font-size: 36rpx;
					font-weight: bold;
					letter-spacing: 2rpx;
					margin-bottom: 30rpx;
				}

				.span_wx {
					color: #999;
					font-size: 28rpx;
					margin: 20rpx 0;
				}

				.btn_wx {
					:deep(button:after) {
						border: none !important;
					}

					.onlogout {
						font-size: 30rpx;
						padding: 0rpx 180rpx;
						height: 80rpx;
						border-radius: 50rpx;
						line-height: 80rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						background-color: #007cff;
					}
				}
			}
		}
	}
</style>
<style lang="scss" scoped>
	.navbar {
		.fixed {
			// position: fixed;
			// top: 0;
			// left: 0;
			// z-index: 99;
			width: 100%;
			// background-color: #007cff;
		}
	}
</style>
